﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Caching;
using System.Xml.Linq;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Models;

namespace Netkk.Data.Repositories
{
    public class RegistrationRepository : BaseRepository, IRegistrationRepository
    {
        private readonly string registrationPath;
        
        protected XDocument RegistrationDocument
        {
            get { return CacheWrapper.Load("registration", new CacheDependency(registrationPath), () => XDocument.Load(registrationPath)); }
        }

        public RegistrationRepository()
        {
            registrationPath = string.Concat(ConnectionString, "Downloads.xml");
        }

        public IEnumerable<Registration> Select()
        {
            IEnumerable<Registration> registration = from r in RegistrationDocument.Element("Registrations").Elements("Registration")
                                                     select getRegistrationFromXElement(r);
            return registration;
        }

        private Registration getRegistrationFromXElement(XElement XRegistration)
        {
            return new Registration
            {
                Id = XRegistration.Element("Id").Value.ToOrDefault<Guid>(),
                AppointmentId = XRegistration.Element("AppointmentId").Value.ToOrDefault<Guid>(),
                UserId = XRegistration.Element("UserId").Value.ToOrDefault<Guid>(),
                DateChanged = XRegistration.Element("DateChanged").Value.ToOrDefault<DateTime>(),
                DateCreated = XRegistration.Element("DateCreated").Value.ToOrDefault<DateTime>(),
                Email = XRegistration.Element("Email").Value,
                FirstName = XRegistration.Element("FirstName").Value,
                LastName = XRegistration.Element("LastName").Value,
                State = XRegistration.Element("State").Value.ToOrDefault<RegistrationState>()
            };
        }

        public void Delete(Registration registration)
        {
            XElement xregistration = (from r in RegistrationDocument.Element("Registrations").Elements("Registration")
                                      where r.Element("Id").Value.ToOrDefault<Guid>() == registration.Id
                                      select r).FirstOrDefault();
            if (xregistration != null)
            {
                xregistration.Remove();
                RegistrationDocument.Save(registrationPath);
            }
        }

        public void Save(Registration registration)
        {
            XElement xregistration = (from r in RegistrationDocument.Element("Registrations").Elements("Registration")
                                      where r.Element("Id").Value.ToOrDefault<Guid>() == registration.Id
                                      select r).FirstOrDefault();
            if (xregistration == null)
            {
                xregistration = new XElement("Registration");
                RegistrationDocument.Element("Registrations").Add(xregistration);
            }
            else
            {
                xregistration.RemoveAll();
            }

            xregistration.Add(
                    new XElement("Id") { Value = registration.Id.ToString("B") },
                    new XElement("AppointmentId") { Value = registration.AppointmentId.ToString("B") },
                    new XElement("UserId") { Value = registration.UserId.ToString("B") },
                    new XElement("DateChanged") { Value = registration.DateChanged.ToString() },
                    new XElement("DateCreated") { Value = registration.DateCreated.ToString() },
                    new XElement("Email") { Value = registration.Email },
                    new XElement("FirstName") { Value = registration.FirstName },
                    new XElement("LastName") { Value = registration.LastName },
                    new XElement("State") { Value = ((int)registration.State).ToString() });

            RegistrationDocument.Save(registrationPath);
        }

    }

}
